//
//  MYGMedicalToolsTIMISTEMIView.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/5/27.
//

import UIKit
import JXSegmentedView

class MYGMedicalToolsTIMISTEMIView: UIView {
    // MARK: - Property
    private var age1: Bool?
    private var age2: Bool?
    private var ill: Bool?
    private var pressure: Bool?
    private var heart: Bool?
    private var killp: Bool?
    private var weight: Bool?
    private var st: Bool?
    private var time: Bool?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        backgroundColor = .white
        addSubviews([
            scrollView,
            commitButton
        ])
        
        scrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(commitButton.snp.top)
        }
        
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
        
        scrollView.addSubview(scrollContentView)
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let line1 = createSeparator()
        let line2 = createSeparator()
        let line3 = createSeparator()
        let line4 = createSeparator()
        let line5 = createSeparator()
        let line6 = createSeparator()
        let line7 = createSeparator()
        let line8 = createSeparator()
        let line9 = createSeparator()

        scrollContentView.addSubviews([
            chooseView1,
            line1,
            chooseView2,
            line2,
            chooseView3,
            line3,
            chooseView4,
            line4,
            chooseView5,
            line5,
            chooseView6,
            line6,
            chooseView7,
            line7,
            chooseView8,
            line8,
            chooseView9,
            line9,
            scoreView
        ])
        
        chooseView1.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview().inset(26)
        }
        
        line1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView1.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView2.snp.makeConstraints { make in
            make.top.equalTo(chooseView1.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView2.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView3.snp.makeConstraints { make in
            make.top.equalTo(chooseView2.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line3.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView3.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView4.snp.makeConstraints { make in
            make.top.equalTo(chooseView3.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line4.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView4.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView5.snp.makeConstraints { make in
            make.top.equalTo(chooseView4.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line5.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView5.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView6.snp.makeConstraints { make in
            make.top.equalTo(chooseView5.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line6.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView6.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView7.snp.makeConstraints { make in
            make.top.equalTo(chooseView6.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line7.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView7.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView8.snp.makeConstraints { make in
            make.top.equalTo(chooseView7.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line8.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView8.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView9.snp.makeConstraints { make in
            make.top.equalTo(chooseView8.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line9.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView9.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        scoreView.snp.makeConstraints { make in
            make.top.equalTo(chooseView9.snp.bottom)
            make.height.equalTo(108)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()

    private lazy var scrollContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var chooseView1: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "年龄≥75岁"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self](race) in
            self?.age1 = race == 0
            if race == 0 {
                self?.chooseView2.selectedIndex = 1
            }
        }
        return view
    }()
    
    private lazy var chooseView2: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "年龄65～74岁"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.age2 = race == 0
            if race == 0 {
                self?.chooseView1.selectedIndex = 1
            }
        }
        return view
    }()
    
    private lazy var chooseView3: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "糖尿病或高血压或心绞痛"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.ill = race == 0
        }
        return view
    }()
    
    private lazy var chooseView4: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "收缩压<100mmHg"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.pressure = race == 0
        }
        return view
    }()
    
    private lazy var chooseView5: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "心率>100次/分"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.heart = race == 0
        }
        return view
    }()
    
    private lazy var chooseView6: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "Killp  II~IV"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.killp = race == 0
        }
        return view
    }()
    
    private lazy var chooseView7: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "体重<67kg"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.weight = race == 0
        }
        return view
    }()
    
    private lazy var chooseView8: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "前壁ST段抬高或左束支传导阻滞"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.st = race == 0
        }
        return view
    }()
    
    private lazy var chooseView9: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "距离就诊时间>4h"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.time = race == 0
        }
        return view
    }()
    
    
    private lazy var scoreView: MYGMedicalToolsScoreView = {
        let view = MYGMedicalToolsScoreView()
        view.level = "正常"
        view.title = "0"
        return view
    }()
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
}


extension MYGMedicalToolsTIMISTEMIView: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        self
    }
}

extension MYGMedicalToolsTIMISTEMIView {
    @objc func commit() {
        guard let age1 = age1, let age2 = age2, let ill = ill, let pressure = pressure, let heart = heart, let killp = killp, let weight = weight, let st = st, let time = time else {
            MMYGHUD.showToast(text: "请先选择所有项")
            return
        }
        
        var score = 0
        if age1 {
            score = score + 3
        } else if age2 {
            score = score + 2
        }
        
        if ill {
            score = score + 1
        }
        
        if pressure {
            score = score + 3
        }
        
        if heart {
            score = score + 2
        }
        
        if killp {
            score = score + 2
        }
        
        if weight {
            score = score + 1
        }
        
        if st {
            score = score + 1
        }
        
        if time {
            score = score + 1
        }
        
        if score <= 3 {
            scoreView.level = "低危"
        } else if score <= 6 {
            scoreView.level = "中危"
        } else {
            scoreView.level = "高危"
        }
        scoreView.progress = Double(score) / 14.0
        scoreView.title = "\(score)"
    }
}
